package ostt1_TokenLib;



public class ASNtype 
{

	public static class tlv_short
	{
	public byte tag;
	public 	int len; //just get 1 byte
	//private int temp_len;
	public 	byte[] data;
		tlv_short()
		{
			tag = 0;len = 0;
		}
		void load(byte[] dat, int dat_len,int scr_index)
		{
			data = new byte[dat_len];
			System.arraycopy(dat, scr_index, data, 0, dat_len);
			len = dat_len;
		}
		void add(byte[] dat, int dat_len,int scr_index)
		{
			
			if(data!=null)
			{
				byte[] tmp = new byte[len];
				System.arraycopy(data, 0, tmp, 0, len);
				data = new byte[dat_len + len];
				System.arraycopy(tmp, 0, data, 0, len);
				System.arraycopy(dat, scr_index, data, len, dat_len);
			}
			else
			{
				data = new byte[dat_len];				
				System.arraycopy(dat, scr_index, data, 0, dat_len);
			}
			len += dat_len;
		}
		void add(tlv_short tlv)
		{
			add(tlv.getByte(),tlv.len+2,0);
		}
		
		byte[] getByte()
		{
			
			byte[] dat = new byte[len+2];
			dat[0] = tag; dat[1] = (byte)(len&0xff);
			if(data!=null)
				System.arraycopy(data, 0, dat, 2, len);
			//memcpy(dat+2, data, len);
			return dat;
		}
	};
	//http://msdn.microsoft.com/en-us/library/bb648644%28v=vs.85%29.aspx
	public static class tlvOctetString81
	{
	public short tag;
	public int len;// just get 1 byte
	public byte []data;
		tlvOctetString81(){ this.tag = 0x0481;} 
		void load(byte[] dat, int dat_len,int scr_index)
		{
			data = new byte[dat_len];			
			//memcpy(data,dat,dat_len);
			System.arraycopy(dat, scr_index, data, 0, dat_len);
			len = dat_len ;
		}
		byte[] getByte()
		{
			
			byte[] dat = new byte[len+3];
			dat[0] = (byte)0x04; dat[1] = (byte)0x81; dat[2] = (byte)(len & 0xff);
			//memcpy(dat+3, data, len);
			if(data!=null)
				System.arraycopy(data, 0, dat, 3, len);
			return dat;
		}
	};
	public static class tlv_long
	{
	public short tag;	
	public int len;// just get 2 byte
	public byte[] data;
		tlv_long()
		{
			tag = 0; len = 0;
		}
		void load(byte[] dat, int dat_len,int scr_index)
		{
			if(dat!=null)
			{
				
				data = new byte[dat_len];			
				System.arraycopy(dat, scr_index, data, 0, dat_len);
			}
			len = dat_len;
		}
		void add(byte[] dat, int dat_len,int scr_index)
		{
			
			if(data!=null)
			{
				byte[] tmp = new byte[len];
				System.arraycopy(data, 0, tmp, 0, len);
				data = new byte[dat_len + len];
				System.arraycopy(tmp, 0, data, 0, len);
				System.arraycopy(dat, scr_index, data, len, dat_len);
			}
			else
			{
				data = new byte[dat_len];				
				System.arraycopy(dat, scr_index, data, 0, dat_len);
			}			
			len +=dat_len;
		}
		void add(tlv_short tlv)
		{
			add(tlv.getByte(),tlv.len+2,0);
		}
		void add(tlv_long tlv)
		{
			add(tlv.getByte(),tlv.len+4,0);
		}
		void add(tlvOctetString81 tlv)
		{
			add(tlv.getByte(),tlv.len+3,0);
		}
		byte[] getByte()
		{
			byte[] dat = new byte[len+4];
			// warming
			//dat[0] = (byte)(tag/0x100); dat[1] = (byte)(tag%0x100);
			//dat[2] = (byte)(len/0x100); dat[3] = (byte)(len%0x100);
			dat[0]=(byte)((tag>>8)&0xff);
	        dat[1]=(byte) (tag & 0xff);
	        dat[2]=(byte)((len>>8)&0xff);
	        dat[3]=(byte) (len & 0xff);
			
			//memcpy(dat+4, data, len);
			if(data!=null)
				System.arraycopy(data, 0, dat, 4, len);
			return dat;
		}
	};
	public static class tlvINT extends tlv_short
	{public	tlvINT(){ this.tag = (byte)0x02; } };

	public static class tlvSET extends tlv_short
	{public	tlvSET(){ this.tag = (byte)0x31; } };

	public static class tlvSETlong extends tlv_long
	{public tlvSETlong(){ this.tag = 0x3182; } };

	public static class tlvSEshort extends tlv_short
	{public tlvSEshort(){ this.tag = (byte)0x30; } };

	public static class tlvSElong extends tlv_long
	{public tlvSElong(){ this.tag = (short)0x3082; } };

	public static class tlvOID extends tlv_short
	{public tlvOID(){ this.tag = (byte)0x06; } };

	public static class tlvNULL extends tlv_short
	{public tlvNULL(){ this.tag = (byte)0x05; this.len = 0; this.data=null;} };

	public static class tlvZero extends tlv_short
	{public tlvZero(){ this.tag = (byte) 0xa0;} };

	public static class tlvZeroLong extends tlv_long
	{public tlvZeroLong(){ this.tag = (short)0xa082; } };

	public static class tlvOctetString extends tlv_short
	{public tlvOctetString(){ this.tag = (byte)0x04;} };
	
	public static class tlvOctetStringLong extends tlv_long
	{public tlvOctetStringLong(){ this.tag = (short)0x0482;} };

	public static class tlvPrintString extends tlv_short
	{public tlvPrintString(){ this.tag = (byte)0x13;} };

	public static class tlvUTCTime extends tlv_short
	{public tlvUTCTime(){ this.tag = (byte)0x17;} };

	public static class tlvUTF8String extends tlv_short
	{public tlvUTF8String(){ this.tag = (byte)0x0C;} };
}
